package com.homedo.odin.solution.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.dao.ProductReqMapper;
import com.homedo.odin.solution.enumcode.usersafe.BizCodeEnum;
import com.homedo.odin.solution.enumcode.usersafe.UserSafeStatusEnum;
import com.homedo.odin.solution.model.entity.ProductReq;
import com.homedo.odin.solution.model.entity.UserBasic;
import com.homedo.odin.solution.model.entity.UserSafe;
import com.homedo.odin.solution.model.request.mng.MngProductOperatorRequestBean;
import com.homedo.odin.solution.model.request.mng.MngProductReqsListRequestBean;
import com.homedo.odin.solution.model.request.pc.ProductReqRequestBean;
import com.homedo.odin.solution.model.response.mng.MngProductReqPageResponseBean;
import java.util.Date;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p> 产品资料需求  服务实现类 </p>
 *
 * @author ZhangLei
 * @date 2018-05-22
 * @Description:
 */
@Service
public class ProductReqServiceImpl extends ServiceImpl<ProductReqMapper, ProductReq> {

    @Autowired
    private UserBasicServiceImpl userBasicService;
    @Autowired
    private UserSafeServiceImpl userSafeService;

    /**
     * Mng_产品资料需求列表
     */
    public MngProductReqPageResponseBean productReqList(MngProductReqsListRequestBean requestBean) {
        PageInfo<ProductReq> pageInfo = PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize())
                .doSelectPageInfo(
                        () -> baseMapper.productReqList(requestBean.getStartTime(), requestBean.getEndTime()));
        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            pageInfo.getList().forEach(dbBean -> {
                UserBasic userBasic = userBasicService.selectByUserId(dbBean.getUserId());
                if (userBasic != null) {
                    dbBean.setUserName(userBasic.getUserName());
                    dbBean.setMobile(userBasic.getMobile());
                }
            });
        }
        return BeanUtils.transfrom(MngProductReqPageResponseBean.class, pageInfo);
    }

    /**
     * Mng_产品资料提交需求
     */
    public void submitProductRequire(ProductReqRequestBean reqRequestBean) {
        UserSafe safe = new UserSafe();
        safe.setBizCode(BizCodeEnum.PRODUCT.getDbValue());
        safe.setUserId(reqRequestBean.getUserId());
        safe.setStatus(UserSafeStatusEnum.APPROVED.getDbValue());
        UserSafe userSafe = userSafeService.selectOne(safe);
        if (userSafe == null) {
            throw new BizException(Meta.CODE_451.getCode(), "未申请白名单，不能提交数据");
        }

        ProductReq req = new ProductReq();
        req.setUserId(reqRequestBean.getUserId());
        req.setContent(reqRequestBean.getContent());
        Date date = new Date();
        req.setCreatedTime(date);
        req.setModifiedTime(date);
        baseMapper.insert(req);
    }

    /**
     * Mng_运营记录
     */
    public void operatorProductReq(Long productId, MngProductOperatorRequestBean requestBean) {
        ProductReq dbData = baseMapper.selectById(productId);
        if (dbData == null) {
            throw new BizException(Meta.CODE_451.getCode(), "数据不存在 productId：" + productId);
        }

        org.springframework.beans.BeanUtils.copyProperties(requestBean, dbData);
        dbData.setModifiedTime(new Date());
        dbData.setModifiedBy(requestBean.getModifiedUserId());
        baseMapper.updateById(dbData);
    }
}
